package com.ruoyi.dynamicmenu;

import java.lang.reflect.Field;

/**
 * **********************
 * * Author: XiaShiLong *
 * * Date  : 2021-05-11 *
 * * Time  : 08:08      *
 * * to    : lz&xm      *
 * **********************
 **/
public class BaseUseStatusBuilder {

    private final static String PK = "id";

    private final static String BASE_USE_STATUS = "dictBaseUseStatus";

    public static <T> void temporary(Long pk, T needInstantiate) {
        instantiate(needInstantiate, pk, BaseUseStatusEnum.TEMPORARY);
    }

    public static <T> void formal(Long pk, T needInstantiate) {
        instantiate(needInstantiate, pk, BaseUseStatusEnum.FORMAL);
    }

    public static <T> void disuse(Long pk, T needInstantiate) {
        instantiate(needInstantiate, pk, BaseUseStatusEnum.DISUSE);
    }

    private static <T> void instantiate(T t, Long pk, BaseUseStatusEnum baseUseStatusEnum) {
        try {
            Field id = t.getClass().getDeclaredField(PK);
            Field dictBaseUseStatus = t.getClass().getDeclaredField(BASE_USE_STATUS);
            id.setAccessible(true);
            dictBaseUseStatus.setAccessible(true);
            id.set(t, pk);
            dictBaseUseStatus.set(t, baseUseStatusEnum.getValue());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException("the object does not contain id or dictBaseUseStatus field!");
        }
    }
}
